package com.infore.airsupervise.ui.activity;


import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

import com.infore.airsupervise.R;
import com.infore.airsupervise.app.AppManager;
import com.infore.airsupervise.app.MsgConst;
import com.infore.airsupervise.bean.UserE;
import com.infore.airsupervise.biz.LoginBiz;
import com.infore.airsupervise.biz.impl.LoginBizImpl;
import com.infore.airsupervise.http.OnServerResponseListener;
import com.infore.airsupervise.utils.JumpUtil;
import com.infore.airsupervise.utils.ToastUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.pedant.SweetAlert.SweetAlertDialog;

/**
 * @features:   用户登录界面
 * @author: WuTao
 * @date: 2017/3/15 17:17
 */
public class LoginActivity extends Activity {

    @BindView(R.id.et_login_name)
    EditText etLoginName;
    @BindView(R.id.et_login_pwd)
    EditText etLoginPwd;

    private LoginBiz loginBiz;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);

        loginBiz = new LoginBizImpl();

        initView();
    }

    @Override
    protected void onResume() {
        super.onResume();
        // 每次启动login界面时清空activity栈
        AppManager.getInstance().finishAllActivity();
    }

    private void initView() {
//        etLoginName.setText("root");
//        etLoginPwd.setText("root");

        etLoginPwd.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_DONE) {
                    loginBtnClick();
                    return true;
                }
                return false;
            }
        });
    }

    /**
     * 登录按钮
     */
    @OnClick(R.id.btn_login)
    public void loginBtnClick() {
        String name = etLoginName.getText().toString();
        String pwd = etLoginPwd.getText().toString();

        if(name.isEmpty() || pwd.isEmpty()){
            ToastUtil.showLong(this, "用户名/密码不能为空");
            return;
        }

        final SweetAlertDialog dialog = new SweetAlertDialog(this, SweetAlertDialog.PROGRESS_TYPE)
                .setTitleText("正在登录")
                .setContentText(MsgConst.PLEASE_WAITING);
        dialog.show();

        loginBiz.sendUserLoginRequest(name, pwd, new OnServerResponseListener<UserE>() {
            @Override
            public void onSucceed(UserE result, String desc) {
                dialog.dismiss();

                //保存登录信息 和登录状态
                UserE.saveUserInfo(result);
                UserE.setLoginStatus(true);
                JumpUtil.jumpFromTo(LoginActivity.this, MainActivity.class);
                finish();
            }

            @Override
            public void onFailed(int errorCode, String title, String desc) {
                dialog.setTitleText(title)
                        .setContentText(desc)
                        .changeAlertType(SweetAlertDialog.WARNING_TYPE);
            }
        });
    }

    @OnClick(R.id.logo_yuxing)
    void goAppCenter() {
        // 打开浏览器下载
        Intent intent= new Intent();
        intent.setAction("android.intent.action.VIEW");
        Uri uri = Uri.parse("http://222.244.144.69:7005/APPProduct/getProduct");
        intent.setData(uri);
        startActivity(intent);
    }

}
